package com.gxuwz.stats.service.excelhandler;

import com.alibaba.excel.EasyExcel;
import com.gxuwz.stats.entity.ScientificActivity;
import com.gxuwz.stats.entity.TotalWork;
import com.gxuwz.stats.service.SheetHandler;
import com.gxuwz.stats.service.SheetName;
import com.gxuwz.stats.service.impl.TotalWorkServiceImpl;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@Service
@SheetName("总工作量")
public class TotalWorkloadHandler implements SheetHandler {
    @Autowired
    private TotalWorkServiceImpl workloadService;

//    @Override
//    public void handle(Sheet sheet) {
//        // 将 Apache POI 的 Sheet 转换为 EasyExcel 的输入流（伪代码）
//        InputStream inputStream = convertSheetToInputStream(sheet);
//        String sheetName = sheet.getSheetName();
//        // 判断 sheetName 是否为空或为默认名（如 Sheet1），决定是否指定
//        if (sheetName != null && !sheetName.trim().isEmpty() && !sheetName.trim().toLowerCase().startsWith("sheet")) {
//            // 指定 sheet 名
//            EasyExcel.read(inputStream, TotalWork.class, new TotalWorkExcelListener(workloadService))
//                    .sheet(sheetName)
//                    .headRowNumber(1)
//                    .doRead();
//        } else {
//            // 不指定 sheet 名，默认第一个
//            EasyExcel.read(inputStream, TotalWork.class, new TotalWorkExcelListener(workloadService))
//                    .sheet()
//                    .headRowNumber(1)
//                    .doRead();
//        }
//        // 使用 EasyExcel 读取数据
////        EasyExcel.read(inputStream, TotalWork.class, new TotalWorkExcelListener(workloadService))
////                .sheet("总工作量")  // 指定 Sheet 名称
////                .headRowNumber(1) // 明确指定标题行
////                .doRead();
//    }

//    @Override
//    public List<String> getHeaderFields() {
//        // 这里写你的表头字段，和Excel第一行一致
//        return Arrays.asList("姓名", "部门", "学位", "职称", "职务", "定额标准", "科研项目分值", "学术著作分值"
//                , "决策咨询类分值", "创作成果类分值", "知识产权类分值", "成果转化类分值", "科研奖项分值", "网络文化类分值",
//                "学术论文分值", "科技活动分值", "重要科研平台分值");
//    }

    private InputStream convertSheetToInputStream(Sheet originalSheet) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            // 创建新工作簿
            Workbook newWorkbook = new SXSSFWorkbook(); // 或 XSSFWorkbook
            // 复制原Sheet内容到新工作簿
            Sheet newSheet = newWorkbook.createSheet(originalSheet.getSheetName());
            copySheet(originalSheet, newSheet); // 需要实现Sheet内容复制逻辑

            newWorkbook.write(out);
            return new ByteArrayInputStream(out.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException("Sheet转换输入流失败", e);
        }
    }

    // 实现Sheet复制逻辑（需处理行和单元格）
    private void copySheet(Sheet src, Sheet dest) {
        for (int i = 0; i < src.getLastRowNum(); i++) {
            Row srcRow = src.getRow(i);
            if (srcRow == null) continue;

            Row destRow = dest.createRow(i);
            for (int j = 0; j < srcRow.getLastCellNum(); j++) {
                Cell srcCell = srcRow.getCell(j);
                if (srcCell == null) continue;

                Cell destCell = destRow.createCell(j);
                switch (srcCell.getCellType()) {
                    case STRING:
                        destCell.setCellValue(srcCell.getStringCellValue());
                        break;
                    case NUMERIC:
                        destCell.setCellValue(srcCell.getNumericCellValue());
                        break;
                    // 其他类型处理...
                }
            }
        }
    }

//    @Override
    public AcademicWorkExcelListener handle(Sheet sheet, String username, String role, String department) {
        return null;
    }
}
